﻿using System.Net.Http;

namespace HaiSpider.Component.Http
{
    /// <summary>
    /// 减少重复操作的网络类
    /// </summary>
    public class HaiHttp
    {
        #region 字段 & 属性
        // 浏览器标识头
        public const string H_UserAgent_Chrome_Windows = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36";
        public const string H_UserAgent_Chrome_IOS = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1";
        public const string H_UserAgent_Chrome_Android = "Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Mobile Safari/537.36";

        public const string H_Content_Type = "application/x-www-form-urlencoded";
        #endregion

        /// <summary>
        /// 创建默认的客户端
        /// </summary>
        /// <returns></returns>
        public static HttpClient CreateDefaultHttpClient()
        {
            var hanlder = new HttpClientHandler()
            {
                UseCookies = false,
                AllowAutoRedirect = false
            };
            return new HttpClient(hanlder);
        }

        /// <summary>
        /// 创建一个默认的请求头Get
        /// </summary>
        /// <returns></returns>
        public static HttpRequestMessage CreateDefaultGetRequest(string url)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, url);
            request.Headers.Add("User-Agent", H_UserAgent_Chrome_Windows);
            request.Headers.Add("Accept", "*/*");
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            return request;
        }

        /// <summary>
        /// 创建一个默认的请求头
        /// </summary>
        /// <returns></returns>
        public static HttpRequestMessage CreateDefaultPostRequest(string url)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            request.Headers.Add("User-Agent", H_UserAgent_Chrome_Windows);
            request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            return request;
        }

    }
}
